//公共方法
var Vue = require('vue');
const  sessionId = sessionStorage.getItem('sessionId');
// Vue.http.options.credentials = true;
console.log('公共方法',sessionId);
// console.log('------------');
function post(url,param,callBack,failBack=null) {
  let  sessionId = sessionStorage.getItem('sessionId');
  if (url.substr(url.length-1,1) == '/') {
    url = `${param}`?`${url}${param}`:url;
    param = ''
  } else {
    for (let item in param) {
      if (!param[item] && param[item] !== false && param[item] !== 0) {
        delete param[item]
      }
    }
  }

  Vue.default.http.post(url,JSON.stringify(param),{headers:{'sessionId':sessionId},emulateJSON: true,credentials: true}).then(res => {
    callBack && callBack(res.body)
  }, res => {
    if(failBack){
      failBack(res.body)
    }
  });
}

function post1(url,param,callBack,failBack=null) {
  url = url+"?";
  let  sessionId = sessionStorage.getItem('sessionId');
  for (let item in param) {
    if (!param[item] && param[item] !== false) {
      delete param[item]
    }
  }
  for (let item in param) {
    url += `${item}=${param[item]}&`
  }
  if (url.substr(url.length-1,1) == '&') {
    url = url.substr(0, url.length-1);
  }
  Vue.default.http.post(url,'',{headers:{'sessionId':sessionId},emulateJSON: true,credentials: true, responseType:'blob'}).then(res => {
    callBack(res.body)
  }, res => {
    if(failBack){
      failBack(res.body)
    }
  });
}

function get(url,param,callBack,failBack=null) {
  const  sessionId = sessionStorage.getItem('sessionId');
  url = `${param}`?`${url}${param}`:url;
  Vue.default.http.get(url,{headers:{'sessionId':sessionId},emulateJSON: true,credentials: true}).then(res => {
    callBack(res.body)
  }, res => {
    if(failBack){
      failBack(res.body)
    }
  });
}

function get1(url,param,callBack,failBack=null) {
  if (param) {
    url = url+"?"
  }
  let  sessionId = sessionStorage.getItem('sessionId');
  for (let item in param) {
    if (!param[item] && param[item] !== false) {
      delete param[item]
    }
  }
  for (let item in param) {
    url += `${item}=${param[item]}&`
  }
  if (url.substr(url.length-1,1) == '&') {
    url = url.substr(0, url.length-1);
  }
  Vue.default.http.get(url,'',{headers:{'sessionId':sessionId},emulateJSON: true,credentials: true, responseType:'blob'}).then(res => {
    callBack(res.body)
  }, res => {
    if(failBack){
      failBack(res.body)
    }
  });
}

function del(url,param = '',callBack,failBack=null){
  const  sessionId = sessionStorage.getItem('sessionId');
  url = `${param}`?`${url}${param}`:url;
  // for(let item in query){
  //     url = `${url}?${item}=${query[item]}`;
  // }
  Vue.default.http.delete(url,{headers:{'sessionId':sessionId},emulateJSON: true,credentials: true}).then(res => {
    callBack(res.body)
  }, res => {
    if(failBack){
      failBack(res.body)
    }
  });
}

function uploadFile(url,param,contentType,callBack,failBack=null) {   // 上传图片到阿里云 / 腾讯云
  Vue.default.http.put(url, param, {headers:{'Content-Type':contentType},emulateJSON: false}).then(res => {
    callBack(res.data)
  }, res => {
    if(failBack){
      failBack(res.data)
    }
  });
}

function uploadVideo(url,param,contentType,onprogress=null,callBack=null,failBack=null) {   // 上传图片到阿里云
  Vue.default.http.put(url, param, { headers:{'Content-Type':contentType},emulateJSON:false,
    progress:(e)=>{
      onprogress(e);
    }
  }).then(res => {
    callBack(res.data)
  }, res => {
    if(failBack){
      failBack(res.data)
    }
  });
}

function setCookie(name, value){
  var Days = 2;
  var exp = new Date();
  exp.setTime(exp.getTime()+Days*24*60*60*1000);
  // document.cookie = name + "=" + escape(value) ;
  document.cookie = name + "="+ escape(value) +";path=/;expires="+ exp.toGMTString();
}

//读取cookie：
function getCookie(name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : null;
}

export {
  post,
  post1,
  get,
  get1,
  del,
  uploadFile,
  uploadVideo,
  getCookie
}
